let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    level:{
        type:Number,
        default:0
    },
    maxLevel:{ //曾经到过的最大等级  用于自动完成等级成长任务
        type:Number,
        default:0
    },
    isPlayGame:{ //用于检测玩家是否进行过游戏
        type:Boolean,
        default:false,
    },
    role : { //角色皮肤
        type : Number,
        default : 300
    },
    bombColor:{ //默认泡泡颜色
        type:Number,
        default:0
    },
    bombColors:{ //已经拥有的彩色泡泡
        type:Array,
        default:[0]
    },
    skins : {  //皮肤，保存可选角色数组
        type : Array,
        default : []
    },
    hasNewYearSkins:{ //是否有新年皮肤未领取
        type:Boolean,
        default:true
    }
});

schema.index({isPlayGame: -1}, {unique: false});

let levelDescriptions=[
    {min:0,max:10,levelName:'幼儿园小班'},
    {min:10,max:30,levelName:'幼儿园中班'},
    {min:30,max:50,levelName:'幼儿园大班'},
    {min:50,max:80,levelName:'小学一年级'},
    {min:80,max:120,levelName:'小学二年级'},
    {min:120,max:160,levelName:'小学三年级'},
    {min:160,max:200,levelName:'小学四年级'},
    {min:200,max:250,levelName:'小学五年级'},
    {min:250,max:300,levelName:'小学六年级'},
    {min:300,max:400,levelName:'初中一年级'},
    {min:400,max:500,levelName:'初中二年级'},
    {min:500,max:600,levelName:'初中三年级'},
    {min:600,max:800,levelName:'高中一年级'},
    {min:800,max:1000,levelName:'高中二年级'},
    {min:1000,max:1200,levelName:'高中三年级'},
    {min:1200,max:1500,levelName:'大学一年级'},
    {min:1500,max:1800,levelName:'大学二年级'},
    {min:1800,max:2100,levelName:'大学三年级'},
    {min:2100,max:2400,levelName:'大学四年级'},
    {min:2400,max:2900,levelName:'研究生一年级'},
    {min:2900,max:3400,levelName:'研究生二年级'},
    {min:3400,max:3900,levelName:'研究生三年级'},
    {min:3900,max:999999999,levelName:'传奇博士生'},
];

let newManSkins=[301,303,304,305,405];
let newWomanSkins=[401,403,404,305,405];

schema.methods={
    async getRandomServer(options){
        let DockerModel=mongoose.models['Docker'];
        let type=0;
        if(!!options.type){
            type=parseInt(options.type);
        }
        // let docker=await DockerModel.findOne({type:type,status:0});
        // if(!docker){
        //       new Error('当前没有服务器在线');
        // }
        // return docker.getMessage();

        let dockers=await DockerModel.find({type:type,v:options.v});
        if(dockers.length<=0){
            throw new Error('当前没有服务器在线');
        }
        let randomIndex=Math.floor(Math.random()*dockers.length);
        return dockers[randomIndex].getMessage();
    },
    getMessage(){
        return {
            level:this.level,
            role:this.role,
            bombColor:this.bombColor,
            skins:this.skins,
            bombColors:this.bombColors,
        }
    },
    selectBombColor(options={color:'0'}){
        options.color=parseInt(options.color);
        this.bombColor=options.color;
    },
    getLevelDescriptions(){
        return levelDescriptions;
    },
    getLevelName(level){
        let levelDescription=levelDescriptions[this.getLevelIndex(level)];
        if(!!levelDescription){
            return levelDescription.levelName;
        }else{
            return '未知等级';
        }
    },
    //获取所有皮肤和当前使用中的皮肤
    getSkinsAndCurrent(options){
        return {
            role:this.role,
            skins:this.getSkins(options),
            bombColor:this.bombColor,  //正在使用的泡泡皮肤
            bombColors:this.bombColors,  //拥有的泡泡皮肤
        }
    },
    getNewYearSkins(options){
        if(this.hasNewYearSkins){
            this.addSkin(302);
            this.addSkin(402);
            this.hasNewYearSkins=false;
        }
    },
    //获取自己已有的皮肤
    getSkins(options){
        let defaultSkin=options.self.sex==1?300:400;
        let skins=[];
        for(let i=0;i<this.skins.length;i++){
            if(!isNaN(this.skins[i])){
                skins.push(this.skins[i]);
            }
        }
        if(skins.indexOf(defaultSkin)<0){
            skins.push(defaultSkin);
        }
        this.skins=skins;
        return skins;
    },
    //获取当前性别对应的新皮肤
    getNewSkin(options){
        let index=Math.floor(Math.random()*5);
        return options.self.sex==1?newManSkins[index]:newWomanSkins[index];
    },
    //判断是否拥有该角色性别对应新皮肤
    // hasNewSkin(options){
    //     let skins=this.getSkins(options);
    //     let defaultNewSkinNumber=newSkins[options.self.sex==1?0:1];
    //     return skins.indexOf(defaultNewSkinNumber)>=0;
    // },
    getLevelIndex(level){
        if(!level){
            level=this.level;
        }
        let levelDescriptionIndex=levelDescriptions.findIndex(item=>{return level>=item.min&&level<item.max;});
        return levelDescriptionIndex;
    },
    addSkin(type){
        if(this.skins.indexOf(type)<0){
            this.skins.push(type);
        }
    },
    //选择皮肤
    selectSkin(options){
        this.role=options.role;
    },
    useSkin(type){
        if(this.skins.indexOf(type)>=0){
            this.role=type;
        }
    },
    //升级
    levelUp(gameUser,level){
        this.level+=level;
        // if(!!levelDescriptions[this.maxLevel]&&this.level>=levelDescriptions[this.maxLevel].max){
        //     //判断是否触发新用户级别，触发则要完成对应任务
        //     if(gameUser.mission.doOneMission(101)){
        //         this.maxLevel++;
        //     }
        // }
    },
    //降级
    levelDown(gameUser,level){
        this.level-=level;
        if(this.level<0){
            this.level=0;
        }
    }
    // async getMessage(options){
    //     let len = this.skin.length;
    //     let i=0;
    //     while(i<this.skin.length){
    //         let skin = this.skin[i]
    //         if(skin.time >=0 && skin.time > Date.now()){
    //             this.skin.splice(i,1);
    //         } else {
    //             i++
    //         }
    //     }
    //     if(this.skin.length != len){
    //         await options.self.save()
    //     }
    //     return {skin:this.skin};
    // },
    // async changeSkin(options){
    //     let skin = this.skin.find(item=>{return item.id == options.id});
    //     if(!skin){
    //         throw new Error('没有这个皮肤')
    //     }
    //     this.ppt.role = options.id;
    //     await options.self.save();
    // }

};

module.exports=schema;